/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.xpath.view;

import offset.nodes.client.chooser.view.NodeChooserDialog;
import java.net.URL;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.dialog.xpath.model.SaveAsXPathSearch;
import offset.nodes.client.view.BaseApplet;

/**
 *
 * @author Walter Lütgenau
 */
public class SaveAsXPathQueryApplet extends BaseApplet {
    NodeChooserDialog dialog = null;
    ServerModel model = null;
    String repository = null;
    String service = null;
    
    /** Creates a new instance of NodeChooserApplet */
    public SaveAsXPathQueryApplet() {
    }
    
    protected String getParentPath(String path) {
        int index = path.lastIndexOf("/");
        if ( index < 0)
            return path;
        return path.substring(0, index);
    }

    protected String getLastPathComponent(String path) {
        int index = path.lastIndexOf("/");
        if ( index < 0)
            return path;
        return path.substring(index+1);
    }

    public ServerModel getModel() {
        return model;
    }

    public String getRepository() {
        return repository;
    }

    public String getService() {
        return service;
    }
    
    public void startUI() {
        dialog.setVisible(true);
        int result = dialog.getReturnStatus();
        try {
            if ( result == NodeChooserDialog.RET_OK) {
                SaveAsXPathSearch.Request request = new SaveAsXPathSearch.Request();
                request.setParent(getParentPath(dialog.getPath()));
                request.setName(getLastPathComponent(dialog.getPath()));
                
                getModel().sendRequest(request);
                
                URL url = new URL(getRepository()+"/"+ dialog.getPath() );
                getAppletContext().showDocument(url);
            }
            else 
                dialog.setVisible(false);
        
        } catch( Exception e) {
            e.printStackTrace();
        }
    }
    
    public void initUI() {
        try {
            this.service = getParameter("service");       
            this.repository = getParameter("repository");
            
            this.model = new ServerModel(new URL(getService()));
            this.dialog = new NodeChooserDialog(new javax.swing.JFrame(), true, model, "/", "*");
            dialog.setLocation(100,100);
        } catch( Exception e) {
            e.printStackTrace();
        }
    }
    
}
